/**
 * created by 小卷毛, 2021-12-06
 * Copyright (c) 2021, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.rzico.sbl.viewmodel

import com.lzg.extend.BaseResponse
import com.lzg.extend.jackson.JacksonConvert
import com.lzg.okrx.RxSingleHelper
import com.lzg.okrx.adapter.FlowableBody
import com.lzy.okgo.OkGo
import com.rzico.sbl.model.DispatchBucket
import com.rzico.sbl.ui.report.other.ReportUrl
import com.xinnuo.common_ui.utils.applyFlowableSchedulers
import com.xinnuo.common_ui.utils.applySchedulers

/**
 * 描 述：新版送达接口
 * 作 者：小卷毛  2021-12-06 15:13
 * 修改描述：
 * 修 改 人：
 * 修改版本：
 */
class ReportDispatchViewModel : CommonViewModel() {

    /**
     * 品牌列表
     */
    fun getBarrelList(itemId: String, isLoading: Boolean = true) =
        OkGo.get<BaseResponse<ArrayList<DispatchBucket>>>(ReportUrl.getBaseUrl().dphBarrel)
            .params(
                getParams("shippingId" to itemId)
            )
            .converter(object : JacksonConvert<BaseResponse<ArrayList<DispatchBucket>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .doOnSubscribe { if (isLoading) showLoading() }
            .doFinally { if (isLoading) dismissLoading() }
            .map { it.data }

    /**
     * 送达 第一步
     */
    fun dispatchFirst(vararg params: Pair<String, Any>) =
        RxSingleHelper.requestString(
            url = ReportUrl.getBaseUrl().dphFirst,
            isMultipart = true,
            params = getParams(*params),
        )
            .compose(applySchedulers())
            .doOnSubscribe { showLoading() }
            .doFinally { dismissLoading() }

    /**
     * 收款列表信息
     */
    fun dispatchPay(shippingSn: String, isLoading: Boolean = true) =
        RxSingleHelper.requestString(
            url = ReportUrl.getBaseUrl().dphPay,
            params = getParams("sn" to shippingSn),
        )
            .compose(applySchedulers())
            .doOnSubscribe { if (isLoading) showLoading() }
            .doFinally { if (isLoading) dismissLoading() }

    /**
     * 送达 第二步
     */
    fun dispatchSecond(vararg params: Pair<String, Any>) =
        RxSingleHelper.requestString(
            url = ReportUrl.getBaseUrl().dphSecond,
            isMultipart = true,
            params = getParams(*params),
        )
            .compose(applySchedulers())
            .doOnSubscribe { showLoading() }
            .doFinally { dismissLoading() }

    /**
     * 送达 第三步
     */
    fun dispatchThird(vararg params: Pair<String, Any>) =
        RxSingleHelper.requestString(
            url = ReportUrl.getBaseUrl().dphThird,
            isMultipart = true,
            params = getParams(*params),
        )
            .compose(applySchedulers())
            .doOnSubscribe { showLoading() }
            .doFinally { dismissLoading() }

    /**
     * 送达 烂桶
     */
    fun dispatchRotten(vararg params: Pair<String, Any>) =
        RxSingleHelper.requestString(
            url = ReportUrl.getBaseUrl().dphSwap,
            isMultipart = true,
            params = getParams(*params),
        )
            .compose(applySchedulers())
            .doOnSubscribe { showLoading() }
            .doFinally { dismissLoading() }

    /**
     * 已送达修改 第一步
     */
    fun modifyFirst(vararg params: Pair<String, Any>) =
        RxSingleHelper.requestString(
            url = ReportUrl.getBaseUrl().depotModify1,
            isMultipart = true,
            params = getParams(*params),
        )
            .compose(applySchedulers())
            .doOnSubscribe { showLoading() }
            .doFinally { dismissLoading() }

}